<?php

namespace app\api\model\circle;


use app\api\model\UserModel;
use app\logic\model\circle\CircleCommentLogicModel;
use app\logic\model\circle\CircleLikeLogicModel;
use app\logic\model\circle\CircleLogicModel;

class CircleApiModel extends CircleLogicModel
{
    public function getApiList(array $param,$where=[],$filed="*",$page=1,$limit=15,$order="id desc"){
        $query = $this->where($where)
            ->field($filed)
            ->where(function($q) use ($param){
                if(check_request($param,'type_id')){
                    $q->where('type_id',$param['type_id']);
                }
                if(check_request($param,'status')){
                    $q->where('status',$param['status']);
                }
                if(check_request($param,'audit')){
                    $q->where('audit',$param['audit']);
                }
            });
        $count = $query->count();
        $list = $query
            ->withCount([
                'likes'=>'like_count',
                'comments'=>'comment_count',
            ])->page($page,$limit)
            ->select()->toArray();
        if(!empty($list)){
            $user_id_arr = array_column($list,'user_id');
            $users = UserModel::getInfosByIds($user_id_arr,'nickname,head_img');
            $type_ids_arr = array_column($list,'type_id');
            $types = CircleTypeApiModel::whereIn('id',$type_ids_arr)->column('title','id');
            $commentModel = new CircleCommentApiModel();
            $likeModel = new CircleLikeLogicModel();
            foreach ($list as &$val){
                $val['user'] = $users[$val['user_id']]??[];
                $val['type'] = $types[$val['type_id']]??[];
                //最新两条评论
                $commentWhere =[
                    'status'=>1,
                    'audit'=>1,
                    'c_id'=>$val['id'],
                ];
                $val['two_comment'] = $commentModel->getApiList($commentWhere,[],'id,user_id,content,apply_id,create_time',1,2,'like_count desc,id desc')[0];

                //最新点赞的头像
                $three_like =$likeModel->where('c_id',$val['id'])
                    ->field('user_id')->select()->toArray();
                if(!empty($three_like)){
                    foreach ($three_like as &$t_val){
                        $t_val['user'] = UserModel::getInfoById($t_val['user_id'],'head_img');
                        unset($t_val['user_id']);
                    }
                    unset($t_val);
                }
                $val['three_like'] = $three_like;



            }
            unset($val);
        }
        return [$list,$count];
    }
    function getFindById($id,$field='*'){
        $info = $this->field($field)->find($id);
        if(empty($info)){
            return false;
        }
        $info['user'] = UserModel::getInfoById($info['user_id'],'nickname,head_img');
        $info['type'] = CircleTypeApiModel::where('id',$info['type_id'])->value('title');
        return $info;
    }
}